package com.open.party.cloud.view.task

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import cn.android.x.parent.AppTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.view.task.fragment.TaskForMeListForAllFragment
import com.open.party.cloud.view.task.fragment.TaskForMeListForDoneFragment
import com.open.party.cloud.view.task.fragment.TaskForMeListForOverTimeFragment
import com.open.party.cloud.view.task.fragment.TaskForMeListForUndoneFragment
import kotlinx.android.synthetic.main.title_tab_view_page.*

class TaskListForMeActivity : AppTitleBaseActivity() {

    override fun getLayoutResId(): Int = R.layout.title_tab_view_page

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("我的任务")

        initTabView()
    }

    private fun initTabView() {
        val titleList = ArrayList<String>()
        val fragmentList = ArrayList<Fragment>()

        titleList.add("全部")
        titleList.add("进行中")
        titleList.add("已延期")
        titleList.add("已完成")

        fragmentList.add(TaskForMeListForAllFragment())
        fragmentList.add(TaskForMeListForUndoneFragment())
        fragmentList.add(TaskForMeListForOverTimeFragment())
        fragmentList.add(TaskForMeListForDoneFragment())

        initTab(
            titleList,
            fragmentList
        )
    }

//    private fun getFragments(typeList: java.util.ArrayList<String>): List<Fragment> {
//        val fragments: ArrayList<Fragment> = ArrayList()
//
//        for (type in typeList) {
//            val fragment = ArticleListFragment()
//            val bundle = Bundle()
//            bundle.putString("type", type)
//            fragment.arguments = bundle
//            fragments.add(fragment)
//
//            Log.e("type", type)
//        }
//
//        return fragments
//    }

    private fun initTab(titles: ArrayList<String>, fragments: List<Fragment>) {

        val fragmentPagerAdapter = object : FragmentPagerAdapter(supportFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(arg0: Int): Fragment {
                return fragments[arg0]
            }
        }

        viewPager.offscreenPageLimit = titles.size - 1
        viewPager.adapter = fragmentPagerAdapter
        //        pager.addOnPageChangeListener(this);

        scrollTab.setTitles(titles, true)
//        scrollTab.setNumber(1, "9", View.VISIBLE)//设置数字红点
        scrollTab.setViewPager(viewPager)
        scrollTab.setOnTabListener { index, _ ->
            viewPager.setCurrentItem(index, true)
        }
    }
}